/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.service.render.exportable;

import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRXmlDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import org.retabel.persistence.entity.document.SectionConfiguration;
import org.retabel.persistence.entity.report.ReportDesign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * Parameter class to hold the information about section configurations and rendered content.
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 21:41 26.09.11
 *
 * @author Burt Parkers
 * @version 1.0
 */
public class JasperReportsExportableDocumentPart extends ExportableDocumentPart<JasperPrint> {

    /** The Logger. */
    private static final Logger logger = LoggerFactory.getLogger(JasperReportsExportableDocumentPart.class);

    /** Uri to the report design. */
    private final URI reportDesignURI;
    /** Size of the design cache. */
    private final int designCacheSize;
    /** The filled jasper print object. */
    private JasperPrint jasperPrint;

    /**
     * Creates a parameter object.
     *
     * @param sectionConfiguration the section configuration.
     * @param reportDesignURI Uri to the report design.
     * @param designCacheSize the design cache size.
     */
    public JasperReportsExportableDocumentPart(SectionConfiguration sectionConfiguration, URI reportDesignURI,
                                               int designCacheSize) {
        super(sectionConfiguration);

        this.reportDesignURI = reportDesignURI;
        this.designCacheSize = designCacheSize;

    }

    /**
     * Does the fill process. Creates from dataContent and configurations a jasper print object.
     *
     * @param dataContent the data content to fill.
     * @throws Exception if a exception occurs during the process.
     */
    public void fill(byte[] dataContent) throws Exception {
        ReportDesign reportDesign = getSectionConfiguration().getReportDesign();

        String packageName = reportDesign.getReportDesignPackage().getName();
        String version = reportDesign.getReportDesignPackage().getVersion();
        String designName = reportDesign.getName();

        ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(dataContent);
        final JRXmlDataSource jrxmlds = new JRXmlDataSource(xmlInputStream);

        Map<String, Object> params = new HashMap<>();

        JasperReportsReportDesignClassLoader classLoader = new JasperReportsReportDesignClassLoader(designCacheSize, packageName, version,
                                                                            reportDesignURI);
        params.put(JRParameter.REPORT_CLASS_LOADER, classLoader);

        JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromLocation(designName, classLoader);

        logger.debug("Loaded Report: {}", jasperReport);

        jasperPrint = JasperFillManager.fillReport(jasperReport, params, jrxmlds);

        logger.debug("Jasper Print: {}", jasperPrint);
    }

    @Override
    public JasperPrint getRenderedContent() {
        return jasperPrint;
    }
}
